//
//  WBBaseViewController.swift
//  陌路微博
//
//  Created by 张继强 on 16/12/2.
//  Copyright © 2016年 张继强. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    /*用户登录和没有登录的标记*/
    var userLogon:Bool = false
    
    /*上啦刷新和下拉加载的判断标识
     上拉状态为true
     下拉刷新状态为false
     */
    var PULLUP:Bool = false
    var tableView:UITableView?
    var refreshControl:UIRefreshControl?
    lazy var navgationBar:UINavigationBar = UINavigationBar.init(frame: CGRect.init(x: 0, y: 0, width:UIScreen.cz_screenWidth(), height: 64))
    lazy var navIteam:UINavigationItem = UINavigationItem()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension WBBaseViewController {
    func setupUI(){
    self.view.backgroundColor = UIColor.cz_random()
    //设置导航条
    self.setNavgationBar()
    //判断用户是否登录创建不同的View
        userLogon ? self.createTable() : self.createVersiteView()
    //创建表格视图
//    self.createTable()
    //关闭初始缩进效果
    self.automaticallyAdjustsScrollViewInsets = false
    }
    //加载数据
    /// 子类去重写该方法
    func loadData(){
        //如果子类不实现任何方法，默认关闭刷新控件的状态
        self.refreshControl?.endRefreshing()
    }
    fileprivate func createVersiteView(){
        let visiterView:WBVisiterView = WBVisiterView.init(frame: self.view.bounds)
        visiterView.backgroundColor = UIColor.init(colorLiteralRed: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        self.view.addSubview(visiterView)
        view.insertSubview(visiterView, belowSubview: navgationBar)
    }
    fileprivate func createTable(){
        tableView = UITableView.init(frame: self.view.bounds, style: .plain)
        self.view.addSubview(tableView!)
        view?.insertSubview(tableView!, belowSubview: navgationBar)
        tableView?.contentInset = UIEdgeInsetsMake(self.navgationBar.bounds.size.height, 0,(tabBarController?.tabBar.bounds.size.height)!,0)
        tableView?.delegate = self
        tableView?.dataSource = self
        self.refreshControl = UIRefreshControl.init()
        self.refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        self.tableView?.addSubview(self.refreshControl!)
    }
    fileprivate func setNavgationBar(){
        self.view.addSubview(self.navgationBar)
        navgationBar.items = [navIteam]
        //将系统的导航栏按钮替换成自定义导航栏按钮
        navIteam.title = self.title
        navgationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        navgationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        //设置导航栏按钮的图片
        navgationBar.tintColor = .orange
    }
}
extension WBBaseViewController{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    @objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell.init()
    }
    //cell将要出现的时候调用
    @objc(tableView:willDisplayCell:forRowAtIndexPath:) func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = (self.tableView?.numberOfSections)! - 1
        if row<0 || section<0 {
            return
        }
        let count = tableView.numberOfRows(inSection: section)
        if (row == count - 1) && (!self.PULLUP) {
            //上拉刷新
            print("上拉刷新")
            self.PULLUP = true
            self.loadData()
        }
    }
}
